name: llm-app - build and public package
on:
  push:
    tags:
      - 'v*.*.*'
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
jobs:
  Build_package:
    name: Build package
    strategy:
      fail-fast: false
    runs-on: ubuntu-22.04
    timeout-minutes: 30
    steps:
      - name: Set up Python 3.11
        uses: actions/setup-python@v5
        with:
            python-version: '3.11'

      - name: Git checkout
        uses: actions/checkout@v4

      - name: Install poetry
        uses: abatilo/actions-poetry@v3

      - name: Build package
        run: poetry build

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: llm-app
          path: ./dist/

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: CHANGELOG.md
          path: CHANGELOG.md

  Verify_package:
    needs: 
      - Build_package
    name: Verify package
    strategy:
      matrix:
        python-version: ["3.10", "3.11"]
      fail-fast: false
    runs-on: ubuntu-22.04
    timeout-minutes: 30
    steps:
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
            python-version: ${{ matrix.python-version }}

      - name: Git checkout
        uses: actions/checkout@v4

      - name: Install poetry
        uses: abatilo/actions-poetry@v3

      - name: create dir for wheels
        run: |
          mkdir wheels

      - uses: actions/download-artifact@v4
        with:
          name: llm-app
          path: ./wheels/

      - name: Build ENV
        run: |
          cat <<EOF > .env
          APP_VARIANT=contextful
          PATHWAY_REST_CONNECTOR_HOST=0.0.0.0
          PATHWAY_REST_CONNECTOR_PORT=8080
          OPENAI_API_KEY=${{ secrets.OPENAI_TOKEN }}
          PATHWAY_CACHE_DIR=/tmp/cache
          EOF

      - name: Install and verify ${{ matrix.os }} package
        run: |
          set -ex
          ENV_NAME="testenv_llm_app"
          rm -rf $ENV_NAME
          python -m venv ${ENV_NAME}
          source ${ENV_NAME}/bin/activate
          pip install python-dotenv
          pip install --prefer-binary wheels/*.whl
          python ./run_examples.py contextful > /dev/null 2>&1 &
          sleep 60
          curl -s --data '{"user": "user", "query": "How to connect to Kafka in Pathway?"}' http://localhost:8080/

  Test_pypi: # test.pypi.org first
    needs:
      - Build_package
      - Verify_package
    name: Test pypi
    strategy:
      fail-fast: false
    runs-on: ubuntu-22.04
    timeout-minutes: 15
    steps:
      - name: Git checkout
        uses: actions/checkout@v4

      - name: Set up Python 3.11
        uses: actions/setup-python@v5
        with:
            python-version: '3.11'

      - name: create dir for wheels
        run: |
          mkdir wheels
  
      - uses: actions/download-artifact@v4
        with:
          name: llm-app
          path: ./wheels/
  
      - uses: actions/download-artifact@v4
        with:
          name: CHANGELOG.md
          path: .

      - name: Build ENV
        run: |
          cat <<EOF > .env
          APP_VARIANT=contextful
          PATHWAY_REST_CONNECTOR_HOST=0.0.0.0
          PATHWAY_REST_CONNECTOR_PORT=8080
          OPENAI_API_KEY=${{ secrets.OPENAI_TOKEN }}
          PATHWAY_CACHE_DIR=/tmp/cache
          EOF  

      # https://github.com/marketplace/actions/pypi-publish
      # https://test.pypi.org/project/llm-app/
      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.TEST_PYPI_TOKEN }}
          packages-dir: './wheels/'
          repository-url: https://test.pypi.org/legacy/
  
      - name: Install and verify ${{ matrix.os }} package
        run: |
          set -ex
          ENV_NAME="testenv_llm_app"
          rm -rf $ENV_NAME
          python -m venv ${ENV_NAME}
          source ${ENV_NAME}/bin/activate
          pip install python-dotenv
          pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple llm-app
          pip show llm-app
          python ./run_examples.py contextful > /dev/null 2>&1 &
          sleep 60
          curl -s --data '{"user": "user", "query": "How to connect to Kafka in Pathway?"}' http://localhost:8080/
  Publish:
    needs:
      - Build_package
      - Verify_package
      - Test_pypi
    environment: PROD
    name: Publish package
    strategy:
      # When true GitHub will cancel all in-progress and queued jobs in the matrix if any job in the matrix fails.
      fail-fast: false
    runs-on: ubuntu-22.04
    timeout-minutes: 15
    steps:
      # Add wheelhouse
      - name: create dir for wheels
        run: |
          mkdir wheels

      - uses: actions/download-artifact@v4
        with:
          name: llm-app
          path: ./wheels/

      - uses: actions/download-artifact@v4
        with:
          name: CHANGELOG.md
          path: .

      - name: Save package to S3
        uses: prewk/s3-cp-action@v2
        with:
          aws_access_key_id: ${{ secrets.ARTIFACT_AWS_ACCESS_KEY_ID }}
          aws_secret_access_key: ${{ secrets.ARTIFACT_AWS_SECRET_ACCESS_KEY }}
          dest: ${{ secrets.ARTIFACT_AWS_BUCKET }}
          source: ./wheels/*.whl
          
      # https://github.com/marketplace/actions/create-release   
      - name: Create Release
        uses: ncipollo/release-action@v1.13.0    
        with:
          draft: true
          artifacts: "./wheels/*.whl"
          artifactContentType: "raw"
          allowUpdates: true
          bodyFile: "CHANGELOG.md"
          tag: ${{github.ref_name}}
          commit: main

      # https://github.com/marketplace/actions/pypi-publish
      # https://pypi.org/project/llm-app/
      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_TOKEN }}
          packages-dir: './wheels/'

  post-release-message:
    runs-on: ubuntu-latest
    needs: Publish
    if: success()
    steps:
      - name: Post to a Slack channel
        id: slack
        uses: slackapi/slack-github-action@v1.23.0
        with:
          # Slack channel id, channel name, or user id to post message.
          # See also: https://api.slack.com/methods/chat.postMessage#channels
          # You can pass in multiple channels to post to by providing a comma-delimited list of channel IDs.
          channel-id: "{{ secrets.SLACK_DEV_CHANNEL_ID }}"
          # For posting a simple plain text message
          payload: |
            {
              "text":  ":tada:\n Hey, it's Manul here. I've made a new release llm-app ${{ github.event.release.tag_name }} released: has been published:\n${{ github.event.release.html_url }} \nhttps://pypi.org/project/llm-app/ \n:tada:",
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text":  ":tada:\n Hey, it's Manul here. I've made a new release llm-app ${{ github.event.release.tag_name }} released: has been published:\n${{ github.event.release.html_url }} \nhttps://pypi.org/project/llm-app/ \n:tada:"
                  }
                }
              ]
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_DEV_CHANNEL_URL }}
